package com.xilihui.xlh.business.activitys;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.HomeEntity;
import com.xilihui.xlh.business.fragments.AssistantFragment;
import com.xilihui.xlh.business.fragments.LectureFragment;
import com.xilihui.xlh.business.fragments.MineFragment;
import com.xilihui.xlh.business.fragments.ShareFragment;
import com.xilihui.xlh.business.fragments.StoreFragment;
import com.xilihui.xlh.component.view.BottomNavigationViewEx;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/16.
 */

public class HomeActivity extends BaseCompatActivity{
    
    @BindView(R.id.navigation)
    BottomNavigationViewEx navigation;
    
    AssistantFragment assistantFragment = new AssistantFragment();
//    ShareFragment shareFragment = new ShareFragment();
//    LectureFragment lectureFragment = new LectureFragment();
    StoreFragment storeFragment = new StoreFragment();
    MineFragment mineFragment = new MineFragment();

    private AMapLocationClient locationClientContinue = null;
    
    @Override
    public int getLayout() {
        return R.layout.activity_home;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    protected void onNewIntent(Intent intent) {// SingleTask模式跳转
        super.onNewIntent(intent);
        // getReceive();
        int intExtra = intent.getIntExtra("logout", 0);
        if (intExtra == 1) {
            setCurrentTab(R.id.navigation_assistan);
        }else if(intExtra == 2){
            setCurrentTab(R.id.navigation_mine);
        }
    }
    
    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        initNavigationView();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        
        //解决Fragment 被回收
        if(savedInstanceState == null){
            //页面重启时不再添加
            transaction.add(R.id.container,assistantFragment,"assistant");
//            transaction.add(R.id.container,shareFragment,"share");
//            transaction.add(R.id.container,lectureFragment,"lecture");
            transaction.add(R.id.container,storeFragment,"store");
            transaction.add(R.id.container,mineFragment,"mine");
        }else {
            assistantFragment = (AssistantFragment)getSupportFragmentManager().findFragmentByTag("assistant");
//            shareFragment = (ShareFragment)getSupportFragmentManager().findFragmentByTag("share");
//            lectureFragment = (LectureFragment)getSupportFragmentManager().findFragmentByTag("lecture");
            storeFragment = (StoreFragment)getSupportFragmentManager().findFragmentByTag("store");
            mineFragment = (MineFragment)getSupportFragmentManager().findFragmentByTag("mine");
        }
        
        transaction.show(assistantFragment)
//                .hide(shareFragment)
//                .hide(lectureFragment)
                .hide(storeFragment)
                .hide(mineFragment)
                .commit();
        
    }

    @Override
    public void initData() {
        
    }
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    public void initNavigationView(){

        navigation.enableShiftingMode(false);
        navigation.enableItemShiftingMode(false);
        navigation.enableAnimation(false);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.navigation_assistan:
                        getSupportFragmentManager().beginTransaction()
                                .show(assistantFragment)
//                                .hide(shareFragment)
//                                .hide(lectureFragment)
                                .hide(storeFragment)
                                .hide(mineFragment)
                                .commit();
                        return true;
//                    case R.id.navigation_share:
//                        getSupportFragmentManager().beginTransaction()
//                                .show(shareFragment)
//                                .hide(assistantFragment)
//                                .hide(lectureFragment)
//                                .hide(storeFragment)
//                                .hide(mineFragment)
//                                .commit();
//                        return true;
//                    case R.id.navigation_lecture:
//                        getSupportFragmentManager().beginTransaction()
//                                .show(lectureFragment)
//                                .hide(shareFragment)
//                                .hide(assistantFragment)
//                                .hide(storeFragment)
//                                .hide(mineFragment)
//                                .commit();
//                        return true;
                    case R.id.navigation_store:
                        getSupportFragmentManager().beginTransaction()
                                .show(storeFragment)
//                                .hide(shareFragment)
//                                .hide(lectureFragment)
                                .hide(assistantFragment)
                                .hide(mineFragment)
                                .commit();
                        return true;
                    case R.id.navigation_mine:
                        
                        if(isLogin()){
                            getSupportFragmentManager().beginTransaction()
                                    .show(mineFragment)
//                                    .hide(shareFragment)
//                                    .hide(lectureFragment)
                                    .hide(storeFragment)
                                    .hide(assistantFragment)
                                    .commit();
                            YEventBuses.post(new YEventBuses.Event("Mine"));
                        }else {
                            startActivity(new Intent(HomeActivity.this,LoginActivity.class));
                        }
                        return true;
                        
                }
                return false;
            }
        });
        
    }
    
    public boolean isLogin(){
        return (boolean) SPUtil.get(this,SPUtil.IS_LOGIN,false);
    }

    /**
     * 启动连续客户端定位
     */
    void startContinueLocation(){
        if(null == locationClientContinue){
            locationClientContinue = new AMapLocationClient(this.getApplicationContext());
        }

        //使用连续的定位方式  默认连续
        AMapLocationClientOption locationClientOption = new AMapLocationClientOption();
        // 地址信息
        locationClientOption.setNeedAddress(true);
        locationClientOption.setInterval(8000);
        locationClientContinue.setLocationOption(locationClientOption);
        locationClientContinue.setLocationListener(locationContinueListener);
        locationClientContinue.startLocation();
    }

    /**
     * 停止连续客户端
     */
    void stopContinueLocation(){
        if(null != locationClientContinue){
            locationClientContinue.stopLocation();
        }
    }

    /**
     * 连续客户端的定位监听
     */
    AMapLocationListener locationContinueListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            LogUtil.i("location","location:"+location.getAddress()+" "
            +location.getLongitude()+location.getLatitude());
            String lat = location.getLatitude()+"";
            String lng = location.getLongitude()+"";
            SPUtil.put(HomeActivity.this,SPUtil.LAT,lat);
            SPUtil.put(HomeActivity.this,SPUtil.LNG,lng);
            LogUtil.i("location","地址："+
                    (String) SPUtil.get(HomeActivity.this,SPUtil.LAT,"0.0")
            +(String)SPUtil.get(HomeActivity.this,SPUtil.LNG,"0.0"));
        }
    };
    
    

    @Override
    protected void onStart() {
        super.onStart();
        startContinueLocation();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopContinueLocation();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);//防止fragment 资源被回收 
        outState.putString("state","防止fragment资源被回收让outState不为null");
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        
        if(event.is("ReLogin")){
            AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
            builder.setMessage((String)event.getParams(0));
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(HomeActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
                
            });
            builder.show();
        }
       
    }
    
    
    public void setCurrentTab(@IdRes int menuId) {
        if (navigation != null) {
            View view = navigation.findViewById(menuId);
            view.performClick();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != locationClientContinue){
            locationClientContinue.onDestroy();
            locationClientContinue = null;
        }
    }
}
